<template>
  <MyAlert 
    type="success" 
    message="角色是一个逻辑集合，你可以授权一个角色某些资源与操作权限，当你将角色授予给用户后，该用户将会继承这个角色中的所有权限。"
  />
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button type="primary" @click="handleCreate"> 
        <Icon icon="ant-design:plus-outlined" /> {{ '新增' }}
      </a-button>
      <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
      <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined"></Icon>
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button>批量操作
          <Icon icon="mdi:chevron-down"></Icon>
        </a-button>
      </a-dropdown>
    </template>
    <!-- <template #toolbar>
      
    </template> -->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>
  </BasicTable>
  <RoleUserTable @register="roleUserDrawer" />
  <RoleDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <RoleDesc @register="registerDesc"></RoleDesc>
  <RolePermissionDrawer @register="rolePermissionDrawer" />
  <RoleApiPermission @register="roleApiPermissionDrawer" />
  <RoleDesignModal @register="registerModal" />
</template>
<script lang="ts" name="system-role" setup>
import { ref } from 'vue'
import { BasicTable, TableAction } from '/@/components/Table';
import { useDrawer } from '/@/components/Drawer';
import { useModal } from '/@/components/Modal';
import RoleDrawer from './components/RoleDrawer.vue';
import RoleDesc from './components/RoleDesc.vue';
import MyAlert from '/@/components/My/MyAlert.vue';
import RolePermissionDrawer from './components/RolePermissionDrawer.vue';
import RoleApiPermission from './components/RoleApiPermission.vue';
import RoleDesignModal from './components/RoleDesignModal.vue';
import RoleUserTable from './components/RoleUserTable.vue';
import { useUserStore } from '/@/store/modules/user';
import { columns, searchFormSchema, RoleEnum } from './role.data';
import { pageRole, deleteRole, batchDeleteRole, getExportUrl, getImportUrl } from '/@/api/sys/role';
import { useListPage } from '/@/hooks/system/useListPage'
import { Icon } from '/@/components/Icon';
import { isManageOrPlatform } from '/@/utils/tenant';

const showFooter = ref(true);
const [roleUserDrawer, { openDrawer: openRoleUserDrawer }] = useDrawer();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerModal, { openModal }] = useModal();
const [rolePermissionDrawer, { openDrawer: openRolePermissionDrawer }] = useDrawer();
const [roleApiPermissionDrawer, { openDrawer: openRoleApiPermissionDrawer }] = useDrawer();
const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer();
const userStore = useUserStore()

// 列表页面公共参数、方法
const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
  designScope: 'role-template',
  tableProps: {
    title: '角色列表',
    api: pageRole,
    columns: columns,
    bordered: false,
    formConfig: {
      schemas: searchFormSchema
    },
    actionColumn: {
      width: 120,
    },
    rowSelection: undefined
  },
  exportConfig: {
    name: "角色列表",
    url: getExportUrl
  },
  importConfig: {
    url: getImportUrl
  }
})
const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

function handleCreate() {
  showFooter.value = true;
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  showFooter.value = true;
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleDetail(record) {
  showFooter.value = false;
  openRoleDesc(true, {
    record,
    isUpdate: true,
  });
}

async function handleDelete(record) {
  await deleteRole({ id: record.id }, reload);
}

async function batchHandleDelete() {
  await batchDeleteRole({ ids: selectedRowKeys.value }, reload);
}

function handleAssignMenuResource(record) {
  openRolePermissionDrawer(true, { roleId: record.id, ...record });
}

function handleAssignApiResource(record) {
  openRoleApiPermissionDrawer(true, { record });
}

function handleUser(record) {
  //onSelectChange(selectedRowKeys)
  openRoleUserDrawer(true, record)
}

function getTableAction(record) {
  return [
    {
      label: '用户',
      ifShow: () => record.roleCode !== RoleEnum.OWNER,
      onClick: handleUser.bind(null, record),
    },
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    }
  ]
}

function getDropDownAction(record) {
  let resp: any = [
    {
      label: '编辑',
      ifShow: () => record.roleCode !== RoleEnum.OWNER,
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '菜单授权',
      ifShow: () => record.roleCode !== RoleEnum.OWNER,
      onClick: handleAssignMenuResource.bind(null, record),
    },
    {
      label: '接口授权',
      ifShow: () => isManageOrPlatform(userStore), // 目前规定接口授权只有平台租户才可以拥有, 即管理员
      onClick: handleAssignApiResource.bind(null, record),
    },
  ]
  if (record.roleCode !== "default") {
    resp.push({
      label: '删除',
      ifShow: () => checkCanDeletd(record),
      popConfirm: {
        title: `是否确认删除 ${record.roleName}`,
        confirm: handleDelete.bind(null, record),
      },
    })
  }

  return resp
}

const checkCanDeletd = (record) => {
  if (record.sysBuiltIn) {
    return false;
  }
  for (const key in RoleEnum) {
    if (record.roleCode == RoleEnum[key]) {
      return false
    }
  }
  return true
}
</script>
